package com.zrwl.poscloud.xo.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrwl.poscloud.base.entity.CompanyPayConfig;
//import com.zrwl.poscloud.base.enums.CompanyServerBusinessType;
import com.zrwl.poscloud.base.enums.EStatus;
import com.zrwl.poscloud.base.superI.SuperServiceImpl;
import com.zrwl.poscloud.commons.holder.RequestHolder;
import com.zrwl.poscloud.commons.utils.JsonUtils;
import com.zrwl.poscloud.commons.utils.RedisUtil;
import com.zrwl.poscloud.commons.utils.ResultUtil;
import com.zrwl.poscloud.commons.utils.StringUtils;
import com.zrwl.poscloud.xo.global.MessageConf;
import com.zrwl.poscloud.xo.global.RedisConf;
import com.zrwl.poscloud.xo.global.SQLConf;
import com.zrwl.poscloud.xo.global.SysConf;
import com.zrwl.poscloud.xo.mapper.CompanyPayConfigMapper;
//import com.zrwl.poscloud.xo.service.AccountFeeConfigService;
//import com.zrwl.poscloud.xo.service.AccountFeeService;
import com.zrwl.poscloud.xo.service.CompanyPayConfigService;
//import com.zrwl.poscloud.xo.service.ServerSyncRecordService;
import com.zrwl.poscloud.xo.vo.CompanyPayConfigVO;
//import com.zrwl.poscloud.xo.vo.ServerSyncRecordVO;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

/**
 * 在线支付商户信息 服务实现类
 *
 * @author 刘思童 AutoGenerate
 * @date 2021-06-11
 */
@Service
public class CompanyPayConfigServiceImpl extends SuperServiceImpl<CompanyPayConfigMapper, CompanyPayConfig> implements CompanyPayConfigService {

    @Autowired
    private CompanyPayConfigService companyPayConfigService;
//    @Autowired
//    private AccountFeeService accountFeeService;
//    @Autowired
//    private AccountFeeConfigService accountFeeConfigService;
    @Autowired
    private RedisUtil redisUtil;
//    @Autowired
//    private ServerSyncRecordService serverSyncRecordService;
    @Resource
    private CompanyPayConfigMapper companyPayConfigMapper;

    //cloud
    @Override
    public IPage<CompanyPayConfig> getPageList(CompanyPayConfigVO companyPayConfigVO) {
        QueryWrapper<CompanyPayConfig> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(companyPayConfigVO.getKeyword()) && !StringUtils.isEmpty(companyPayConfigVO.getKeyword().trim())) {
            queryWrapper.like("merchant_name", companyPayConfigVO.getKeyword().trim()).or()
                    .like("merchant_code", companyPayConfigVO.getKeyword().trim());
        }
        HttpServletRequest request = RequestHolder.getRequest();
        String ownerUid = null;
        if (request.getAttribute(SysConf.OWNER_UID) != null) {
            ownerUid = request.getAttribute(SysConf.OWNER_UID).toString();
            queryWrapper.eq("owner_uid", ownerUid);
        }
        if (companyPayConfigVO.getWayType() != 0) {
            queryWrapper.eq("way_type", companyPayConfigVO.getWayType());
        }
        if (companyPayConfigVO.getPlatType() != 0) {
            queryWrapper.eq("plat_type", companyPayConfigVO.getPlatType());
        }
        Page<CompanyPayConfig> page = new Page<>();
        page.setCurrent(companyPayConfigVO.getCurrentPage());
        page.setSize(companyPayConfigVO.getPageSize());
        queryWrapper.ne(SQLConf.STATUS, EStatus.DISABLED);
        queryWrapper.orderByDesc(SQLConf.CREATE_TIME);
        IPage<CompanyPayConfig> pageList = companyPayConfigService.page(page, queryWrapper);
        List<CompanyPayConfig> list = pageList.getRecords();
        pageList.setRecords(list);
        return pageList;
    }

    @Override
    public List<CompanyPayConfig> getAllList(CompanyPayConfigVO companyPayConfigVO) {
        QueryWrapper<CompanyPayConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne(SQLConf.STATUS, EStatus.DISABLED);
        queryWrapper.orderByAsc(SQLConf.CREATE_TIME);
        List<CompanyPayConfig> list = companyPayConfigService.list(queryWrapper);
        return list;
    }

//    @Override
//    public String add(CompanyPayConfigVO companyPayConfigVO) {
//        QueryWrapper<CompanyPayConfig> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("merchant_code", companyPayConfigVO.getMerchantCode());
//        queryWrapper.eq("plat_type", companyPayConfigVO.getPlatType());
//        queryWrapper.eq("way_type", companyPayConfigVO.getWayType());
//        if (companyPayConfigService.count(queryWrapper) > 0) {
//            return ResultUtil.errorWithMessage(MessageConf.INSERT_SUCCESS_REPEAT);
//        }
//        companyPayConfigVO.setOwnerType(2);//固定商户
//        companyPayConfigVO.setOwnerUid(companyPayConfigVO.getOwnerUid());
//        CompanyPayConfig entity = new CompanyPayConfig();
//        BeanUtils.copyProperties(companyPayConfigVO, entity, SysConf.STATUS);
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("fee", accountFeeService.getById(entity.getFeeUid()));
//        jsonObject.put("feeConfig", accountFeeConfigService.getById(entity.getFeeConfigUid()));
//        entity.setFeeConfigJsonData(jsonObject.toJSONString());
//        entity.insert();
//        companyPayConfigVO.setUid(entity.getUid());
//        //向下同步
//        ServerSyncRecordVO serverSyncRecordVO = new ServerSyncRecordVO();//加入同步数据
//        serverSyncRecordVO.setObject(entity);
//        serverSyncRecordVO.setCompanyUid(entity.getOwnerUid());
//        serverSyncRecordVO.setSummary(CompanyServerBusinessType.companyFeeConfigDown.getName());
//        serverSyncRecordVO.setName(CompanyServerBusinessType.companyFeeConfigDown.getName());
//        serverSyncRecordVO.setType(CompanyServerBusinessType.companyFeeConfigDown.getServerBusinessType());
//        serverSyncRecordService.add(serverSyncRecordVO);
//        return ResultUtil.successWithMessage(MessageConf.INSERT_SUCCESS);
//    }

//    @Override
//    public String edit(CompanyPayConfigVO companyPayConfigVO) {
//        CompanyPayConfig entity = companyPayConfigService.getById(companyPayConfigVO.getUid());
//        BeanUtils.copyProperties(companyPayConfigVO, entity);
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("fee", accountFeeService.getById(entity.getFeeUid()));
//        jsonObject.put("feeConfig", accountFeeConfigService.getById(entity.getFeeConfigUid()));
//        entity.setFeeConfigJsonData(jsonObject.toJSONString());
//        //entity.setUpdateTime//已通过填充实现，后续可删除(new Date());
//        entity.updateById();
//        //向下同步
//        ServerSyncRecordVO serverSyncRecordVO = new ServerSyncRecordVO();//加入同步数据
//        serverSyncRecordVO.setObject(entity);
//        serverSyncRecordVO.setCompanyUid(entity.getOwnerUid());
//        serverSyncRecordVO.setSummary(CompanyServerBusinessType.companyFeeConfigDown.getName());
//        serverSyncRecordVO.setName(CompanyServerBusinessType.companyFeeConfigDown.getName());
//        serverSyncRecordVO.setType(CompanyServerBusinessType.companyFeeConfigDown.getServerBusinessType());
//        serverSyncRecordService.add(serverSyncRecordVO);
//        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
//    }

//    @Override
//    public String delete(CompanyPayConfigVO companyPayConfigVO) {
//        CompanyPayConfig entity = companyPayConfigService.getById(companyPayConfigVO.getUid());
//        entity.setStatus(EStatus.DISABLED);
//        //entity.setUpdateTime//已通过填充实现，后续可删除(new Date());
//        entity.updateById();
//        //向下同步
//        ServerSyncRecordVO serverSyncRecordVO = new ServerSyncRecordVO();//加入同步数据
//        serverSyncRecordVO.setObject(entity);
//        serverSyncRecordVO.setCompanyUid(entity.getOwnerUid());
//        serverSyncRecordVO.setSummary(CompanyServerBusinessType.companyFeeConfigDown.getName());
//        serverSyncRecordVO.setName(CompanyServerBusinessType.companyFeeConfigDown.getName());
//        serverSyncRecordVO.setType(CompanyServerBusinessType.companyFeeConfigDown.getServerBusinessType());
//        serverSyncRecordService.add(serverSyncRecordVO);
//        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
//    }

    @Override
    public Map<String, String> getMerchantCodeList() {
        List<CompanyPayConfig> list = companyPayConfigService.list();
        Map<String, String> map = new HashMap<>();
        if (list != null && list.size() > 0) {
            map.put(list.get(0).getMerchantCode(), list.get(0).getMerchantCode());
            for (CompanyPayConfig items : list) {
                if (map.containsKey(items.getMerchantCode())) {

                } else {
                    map.put(items.getMerchantCode(), items.getMerchantCode());
                }
            }
            return map;
        } else {
            return null;
        }

    }

    //local

    @Override
    public String editLocal(CompanyPayConfigVO companyPayConfigVO) {
        CompanyPayConfig entity = companyPayConfigService.getById(companyPayConfigVO.getUid());
        BeanUtils.copyProperties(companyPayConfigVO, entity);
        //entity.setUpdateTime//已通过填充实现，后续可删除(new Date());
        entity.updateById();
        CompanyPayConfigVO tempVO = new CompanyPayConfigVO();
        tempVO.setOwnerUid(entity.getOwnerUid());
        redisUtil.delete(RedisConf.COMPANY_ONLINE_PAY_CONFIG + RedisConf.SEGMENTATION + tempVO.getOwnerUid());
        getCompanyPayConfigListForMobile(tempVO);
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }

    @Override
    public IPage<CompanyPayConfigVO> getCompanyPayConfigList(CompanyPayConfigVO companyPayConfigVO) {
        Page<CompanyPayConfigVO> page = new Page<>();
        page.setCurrent(companyPayConfigVO.getCurrentPage());
        page.setSize(companyPayConfigVO.getPageSize());
        IPage<CompanyPayConfigVO> pageList = companyPayConfigMapper.getCompanyPayConfigList(page, companyPayConfigVO);
        List<CompanyPayConfigVO> list = pageList.getRecords();
        for (CompanyPayConfigVO payConfigVO : list) {
            if (payConfigVO.getChargeType() != null) {
                payConfigVO.setChargeTypeList(ArrayUtils.toPrimitive(Stream.of(payConfigVO.getChargeType().split(",")).mapToInt(Integer::parseInt).boxed().toArray(Integer[]::new)));
            }
        }
        pageList.setRecords(list);
        return pageList;
    }

    @Override
    public List<CompanyPayConfigVO> getCompanyPayConfigListForMobile(CompanyPayConfigVO companyPayConfigVO) {
        String jsonStr = redisUtil.get(RedisConf.COMPANY_ONLINE_PAY_CONFIG + RedisConf.SEGMENTATION + companyPayConfigVO.getOwnerUid());
        if (jsonStr == null) {
            List<CompanyPayConfigVO> resultList = companyPayConfigMapper.getCompanyPayConfigListForMobile(companyPayConfigVO);
            redisUtil.setEx(RedisConf.COMPANY_ONLINE_PAY_CONFIG + RedisConf.SEGMENTATION + companyPayConfigVO.getOwnerUid(), JsonUtils.objectToJson(resultList), 24, TimeUnit.HOURS);
            return resultList;
        } else {
            return JsonUtils.jsonToList(jsonStr, CompanyPayConfigVO.class);
        }
    }

}
